﻿using System;
using System.Net;
using System.Text;
using System.IO;

namespace AMing.Helper.WindowsPhone8
{
    public class HttpHelper
    {
        /// <summary>
        /// HttpPost(异步)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="callback"></param>
        public void Post_Async(string url, string postData, AsyncCallback callback)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                request.UserAgent = "WPPhone";
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                if (!string.IsNullOrEmpty(postData))
                {
                    byte[] postdata = Encoding.UTF8.GetBytes(postData);
                    request.BeginGetRequestStream(async =>
                    {
                        try
                        {
                            using (Stream stream = request.EndGetRequestStream(async))
                            {
                                stream.Write(postdata, 0, postdata.Length);
                            }
                            request.BeginGetResponse(callback, request);
                        }
                        catch { }
                    },
                    null);
                }
            }
            catch { }
        }
        /// <summary>
        /// HttpPost(异步)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="callback"></param>
        public void Post_Async(string url, string postData, Action<string> callback)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                request.UserAgent = "WPPhone";
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                if (!string.IsNullOrEmpty(postData))
                {
                    byte[] postdata = Encoding.UTF8.GetBytes(postData);
                    request.BeginGetRequestStream(async =>
                    {
                        try
                        {
                            using (Stream stream = request.EndGetRequestStream(async))
                            {
                                stream.Write(postdata, 0, postdata.Length);
                            }
                            request.BeginGetResponse(async2 =>
                            {
                                try
                                {
                                    WebResponse rep = request.EndGetResponse(async2);
                                    using (Stream stream = rep.GetResponseStream())
                                    using (StreamReader sr = new StreamReader(stream))
                                    {
                                        string content = sr.ReadToEnd();
                                        callback(content);
                                    }
                                }
                                catch
                                {
                                    callback(null);
                                }
                            }, null);
                        }
                        catch
                        {
                            callback(null);
                        }
                    },
                    null);
                }
            }
            catch
            {
                callback(null);
            }
        }

        /// <summary>
        /// HttpGet(异步)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="callback"></param>
        public void Get_Async(string url, Action<string> callback)
        {
            HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            try
            {
                request.BeginGetResponse((iar) =>
                {
                    try
                    {
                        HttpWebResponse response = request.EndGetResponse(iar) as HttpWebResponse;
                        if (response != null)
                        {
                            using (var stream = response.GetResponseStream())
                            {
                                using (StreamReader sr = new StreamReader(stream))
                                {
                                    string searchContent = string.Empty;
                                    searchContent = sr.ReadToEnd();
                                    if (callback != null)
                                    {
                                        callback(searchContent);
                                        return;
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                        callback(null);
                        return;
                    }
                }, null);
            }
            catch
            {
                callback(null);
                return;
            }
        }

    }
}
